package com.wyf.video

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.AbsListView
import android.widget.AdapterView
import android.widget.SearchView
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.wyf.video.adapter.NewAdapter
import com.wyf.video.adapter.SearchResultAdapter
import com.wyf.video.global.AppSettings
import com.wyf.video.global.Const
import com.wyf.video.model.Bt
import com.wyf.video.model.Result
import com.zhy.http.okhttp.OkHttpUtils
import com.zhy.http.okhttp.callback.StringCallback
import kotlinx.android.synthetic.main.activity_search.*
import kotlinx.android.synthetic.main.fragment_new.*
import okhttp3.Call
import org.jetbrains.anko.listView
import org.jetbrains.anko.sdk25.coroutines.*
import org.jetbrains.anko.support.v4.toast
import org.jetbrains.anko.toast

class SearchActivity : AppCompatActivity() {

    var page =0
    var totalPage =-1
    lateinit var adapter:SearchResultAdapter
    var btList = ArrayList<Bt>()
    var kw=""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_search)

        search()
        initListView()
        initEvent()
    }

    private fun initEvent() {
        search_ibtn_back.onClick {
            finish()
        }

    }

    private fun initListView() {
        adapter = SearchResultAdapter(this@SearchActivity,btList)
        search_listview.adapter = adapter

        search_listview.setOnItemClickListener { adapterView, view, i, l ->
            val intent = Intent()
            intent.setClass(this, DetailActivity::class.java!!)

            intent.putExtra(Const.PASS_BT, Gson().toJson(btList.get(i)))
            startActivity(intent)

        }

        search_listview.setOnScrollListener(object : AbsListView.OnScrollListener {
            override fun onScroll(p0: AbsListView?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onScrollStateChanged(p0: AbsListView?, p1: Int) {
                if(p1 == AbsListView.OnScrollListener.SCROLL_STATE_IDLE){
                    if (search_listview?.getLastVisiblePosition() == (search_listview.getCount() - 1)) {
                        loadData(page++)
                    }
                }
            }
        })

    }

    private fun search() {

        search_searchview.onQueryTextListener {

            onQueryTextSubmit {
                kw =search_searchview.query.toString()
                btList.clear()
                page=0
                loadData(page++)
                return@onQueryTextSubmit true
            }
        }
    }

    private fun loadData(page:Int) {
        if(page>=totalPage&&totalPage>0){
            toast(R.string.last_page_tip)
            return
        }

//        val url = "http://47.100.23.73:1324/search/"
        val url = "http://47.100.23.73:1324/search/"+page+"/"+AppSettings.LIMIT+"/"+kw
        Log.i("page",url)

        OkHttpUtils
                .get()
                .url(url)
                .build()
                .execute(object : StringCallback() {
                    override fun onError(call: Call?, e: java.lang.Exception?, id: Int) {
                        Log.i("wyf",""+e.toString())
                    }

                    override fun onResponse(response: String?, id: Int) {
                        Log.i("wyf",""+response)
                        var g = Gson()
                        var result = g.fromJson<Result>(response,object : TypeToken<Result>() {}.type)
                        if(result.code==0){

                            btList.addAll(result.data)
                            if(adapter==null){
                                adapter = SearchResultAdapter(this@SearchActivity,btList)
                            }
                            totalPage = result.total_page
                            adapter.notifyDataSetChanged()
                        }else if (result.code == 1){
                            toast(result.msg)
                        }

                    }
                })
    }
}

